﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using RIA.ViewModel;

namespace RIA.NTiersApp.Views
{
    public partial class CustomerDetailsPage : Page
    {

        private RIA.ViewModel.CustomerDetailsViewModel customerVM = null;
        public CustomerDetailsPage ()
        {
            
            InitializeComponent ();
            customerVM = this.DataContext as CustomerDetailsViewModel;
            customerVM.OnSavedData += new EventHandler ( customerVM_OnSavedData );
        }

        void customerVM_OnSavedData ( object sender, EventArgs e )
        {
            //customerVM.customerVM.LoadCustomerDetailsExecute ( CustomerID );
        }
        

        #region CustomerID
        private int CustomerID
        {
            get
            {
                int paramValue = 0;

                const string paramName = "CustomerID";
                string test = NavigationContext.QueryString [ paramName ];
                if ( NavigationContext.QueryString.ContainsKey ( paramName ) )
                    int.TryParse ( NavigationContext.QueryString [ paramName ], out paramValue );
                return paramValue;
            }
        }
        #endregion

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo ( NavigationEventArgs e )
        {
            if ( CustomerID <= 0 )
            {
               customerVM.Customer = customerVM.CreateCustomer ();
               customerVM.AddCustomer ( customerVM.Customer );
            }
            else
            {
                customerVM.LoadCustomerDetailsExecute ( CustomerID );
            }
            
        }

    }
}
